
package com.ami.kvm.jviewer.soc;

import java.awt.Rectangle;
import java.io.DataInputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;

import com.ami.kvm.jviewer.common.ISOCFrameHdr;
import com.ami.kvm.jviewer.common.ISOCJVVideo;
import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.gui.JViewerView;
import com.ami.kvm.jviewer.kvmpkts.IVTPPktHdr;
import com.ami.kvm.jviewer.soc.video.Decoder;
import com.ami.kvm.jviewer.soc.video.VideoEngineInfo;

public class SOCJVVideo implements ISOCJVVideo {
	private byte[] videoBuf;
	private JViewerView viewerView;
	private SOCFrameHdr frameHdr;
	private VideoEngineInfo videoEngInfo;
	private Decoder decoder;
	private Rectangle prevCursorRect;
	public byte[] prevCursorFrame;
	private static boolean m_bFirstInit = true;

	public SOCJVVideo() {
		this.viewerView = JViewerApp.getInstance().getRCView();
	}

	public void VideoBuffer_Initialize() {
		this.videoBuf = ((SOCCreateBufferImage) JViewerApp.getInstance().getPrepare_buf()).imageMemoryByte();
		Arrays.fill(this.videoBuf, (byte) 0);

		this.decoder = new Decoder(this.viewerView, this.videoBuf, this);
		this.prevCursorFrame = new byte['　'];
		this.prevCursorRect = new Rectangle(0, 0, 0, 0);
	}

	public void Newframevalidate() {
		this.videoEngInfo = this.frameHdr.getFrameVariables();
	}

	public ByteBuffer decompressframe(ISOCFrameHdr paramISOCFrameHdr, ByteBuffer paramByteBuffer) {
		this.videoBuf = ((SOCCreateBufferImage) JViewerApp.getInstance().getPrepare_buf()).imageMemoryByte();
		this.decoder.setBuf(this.videoBuf);

		if (this.frameHdr.getCompressedBuf() != null) {
			int[] arrayOfInt = MakeIntArray(this.frameHdr.getCompressedBuf());
			this.decoder.decode(this.videoEngInfo, arrayOfInt);
			this.viewerView.repaint();
		}

		return paramByteBuffer;
	}

	public void SOC_Specific(ByteBuffer paramByteBuffer) {
	}

	public void setframehdr(ISOCFrameHdr paramISOCFrameHdr) {
		this.frameHdr = ((SOCFrameHdr) paramISOCFrameHdr);
	}

	public SOCFrameHdr getframehdr() {
		return this.frameHdr;
	}

	public void SOCBlankscreen() {
	}

	public void SOCreset() {
	}

	public void soccompressionchange() {
	}

	public int[] MakeIntArray(byte[] paramArrayOfByte) {
		int i = 0;
		byte[] arrayOfByte;
		if (paramArrayOfByte.length % 4 == 3) {
			arrayOfByte = new byte[paramArrayOfByte.length + 1];
			System.arraycopy(paramArrayOfByte, 0, arrayOfByte, 0, paramArrayOfByte.length);
			arrayOfByte[(arrayOfByte.length - 1)] = 0;
		} else if (paramArrayOfByte.length % 4 == 2) {
			arrayOfByte = new byte[paramArrayOfByte.length + 2];
			System.arraycopy(paramArrayOfByte, 0, arrayOfByte, 0, paramArrayOfByte.length);
			arrayOfByte[(arrayOfByte.length - 2)] = 0;
			arrayOfByte[(arrayOfByte.length - 1)] = 0;
		} else if (paramArrayOfByte.length % 4 == 1) {
			arrayOfByte = new byte[paramArrayOfByte.length + 3];
			System.arraycopy(paramArrayOfByte, 0, arrayOfByte, 0, paramArrayOfByte.length);
			arrayOfByte[(arrayOfByte.length - 3)] = 0;
			arrayOfByte[(arrayOfByte.length - 2)] = 0;
			arrayOfByte[(arrayOfByte.length - 1)] = 0;
		} else {
			arrayOfByte = new byte[paramArrayOfByte.length];
			System.arraycopy(paramArrayOfByte, 0, arrayOfByte, 0, paramArrayOfByte.length);
		}

		int[] arrayOfInt = new int[arrayOfByte.length / 4];
		for (int j = 0; j < arrayOfByte.length / 4; j++) {
			arrayOfInt[j] = ((arrayOfByte[(i + 3)] & 0xFF) << 24);
			arrayOfInt[j] |= (arrayOfByte[(i + 2)] & 0xFF) << 16;
			arrayOfInt[j] |= (arrayOfByte[(i + 1)] & 0xFF) << 8;
			arrayOfInt[j] |= (arrayOfByte[(i + 0)] & 0xFF) << 0;
			i += 4;
		}

		return arrayOfInt;
	}

	public void SetPointInSavedScreen(int paramInt1, int paramInt2, byte paramByte1, byte paramByte2, byte paramByte3) {
		if (this.prevCursorRect.contains(paramInt1, paramInt2)) {
			int i = ((paramInt2 - this.prevCursorRect.y) * this.prevCursorRect.width
					+ (paramInt1 - this.prevCursorRect.x)) * 3;
			this.prevCursorFrame[i] = paramByte1;
			this.prevCursorFrame[(i + 1)] = paramByte2;
			this.prevCursorFrame[(i + 2)] = paramByte3;
		}
	}

	public void SaveScreenContent(Rectangle paramRectangle) {
		int i = 0;

		int j = 0;
		for (int k = 0; j < paramRectangle.height; j++) {
			for (int m = 0; m < paramRectangle.width; m++) {
				i = ((j + paramRectangle.y) * this.viewerView.viewWidth() + (m + paramRectangle.x)) * 3;
				this.prevCursorFrame[k] = this.videoBuf[i];
				this.prevCursorFrame[(k + 1)] = this.videoBuf[(i + 1)];
				this.prevCursorFrame[(k + 2)] = this.videoBuf[(i + 2)];
				k += 3;
			}
		}
		this.prevCursorRect.setBounds(paramRectangle);
	}

	public void RestoreScreenContent(Rectangle paramRectangle) {
		int i = 0;

		int j = 0;
		for (int k = 0; j < paramRectangle.height; j++) {
			for (int m = 0; m < paramRectangle.width; m++) {
				i = ((j + paramRectangle.y) * this.viewerView.viewWidth() + (m + paramRectangle.x)) * 3;
				this.videoBuf[i] = this.prevCursorFrame[k];
				this.videoBuf[(i + 1)] = this.prevCursorFrame[(k + 1)];
				this.videoBuf[(i + 2)] = this.prevCursorFrame[(k + 2)];
				k += 3;
			}
		}
	}

	private void sendIVTPPacket(IVTPPktHdr paramIVTPPktHdr, DataInputStream paramDataInputStream) {
	}

	public void socreadframeattributes(DataInputStream paramDataInputStream) {
	}
}

/*
 * Location:
 * D:\Users\tianlin\Downloads\JViewer-SOC.jar!\com\ami\kvm\jviewer\soc\
 * SOCJVVideo.class Java compiler version: 5 (49.0) JD-Core Version: 0.7.1
 */